#!/bin/bash

# 盲盒购买API测试脚本
# 使用方法: bash test_blindbox_buy.sh

# 配置
BASE_URL="http://your-domain.com"  # 请修改为您的域名
TOKEN="your_customer_token"         # 请修改为实际的用户token

echo "=========================================="
echo "盲盒购买API测试脚本"
echo "=========================================="
echo ""

# 颜色输出
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 测试1: 获取盲盒列表
echo -e "${YELLOW}测试1: 获取盲盒列表${NC}"
echo "请求: GET ${BASE_URL}/api/blindbox.get_list"
curl -X GET "${BASE_URL}/api/blindbox.get_list" \
  -H "Content-Type: application/json" \
  | python -m json.tool
echo ""
echo "----------------------------------------"
echo ""

# 测试2: 获取盲盒详情
echo -e "${YELLOW}测试2: 获取盲盒详情 (blind_box_id=1)${NC}"
echo "请求: GET ${BASE_URL}/api/blindbox.detail?blind_box_id=1"
curl -X GET "${BASE_URL}/api/blindbox.detail?blind_box_id=1" \
  -H "Content-Type: application/json" \
  | python -m json.tool
echo ""
echo "----------------------------------------"
echo ""

# 测试3: 购买盲盒（快递配送）
echo -e "${YELLOW}测试3: 购买盲盒 - 快递配送${NC}"
echo "请求: POST ${BASE_URL}/api/blindbox.buy"
RESPONSE=$(curl -s -X POST "${BASE_URL}/api/blindbox.buy" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d "blind_box_id=1" \
  -d "quantity=1" \
  -d "shipping_method=package" \
  -d "payment_method=wechat" \
  -d 'address={"name":"测试用户","telephone":"13800138000","province_name":"广东省","city_name":"深圳市","area_name":"南山区","detail_address":"科技园"}' \
  -d "message=测试订单")

echo "$RESPONSE" | python -m json.tool

# 提取order_id
ORDER_ID=$(echo "$RESPONSE" | python -c "import sys, json; data=json.load(sys.stdin); print(data.get('order_id', 0))" 2>/dev/null)

if [ "$ORDER_ID" != "0" ] && [ -n "$ORDER_ID" ]; then
    echo -e "${GREEN}✓ 订单创建成功! Order ID: ${ORDER_ID}${NC}"
else
    echo -e "${RED}✗ 订单创建失败${NC}"
fi
echo ""
echo "----------------------------------------"
echo ""

# 测试4: 购买盲盒（自提）
echo -e "${YELLOW}测试4: 购买盲盒 - 自提${NC}"
echo "请求: POST ${BASE_URL}/api/blindbox.buy"
curl -X POST "${BASE_URL}/api/blindbox.buy" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d "blind_box_id=1" \
  -d "quantity=1" \
  -d "shipping_method=pickup" \
  -d "payment_method=wechat" \
  -d "pickup_person=测试用户" \
  -d "pickup_phone=13800138000" \
  -d 'pickup={"id":1,"name":"总店","phone":"0755-12345678"}' \
  | python -m json.tool
echo ""
echo "----------------------------------------"
echo ""

# 测试5: 查看我的抽奖记录
echo -e "${YELLOW}测试5: 查看我的抽奖记录${NC}"
echo "请求: GET ${BASE_URL}/api/blindbox.my_records"
curl -X GET "${BASE_URL}/api/blindbox.my_records" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TOKEN}" \
  | python -m json.tool
echo ""
echo "----------------------------------------"
echo ""

# 异常测试
echo -e "${YELLOW}异常测试: 缺少必填参数${NC}"
echo "请求: POST ${BASE_URL}/api/blindbox.buy (无blind_box_id)"
curl -X POST "${BASE_URL}/api/blindbox.buy" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "Authorization: Bearer ${TOKEN}" \
  -d "quantity=1" \
  | python -m json.tool
echo ""
echo "----------------------------------------"
echo ""

echo -e "${GREEN}测试完成!${NC}"
echo ""
echo "注意事项:"
echo "1. 请修改脚本中的 BASE_URL 为您的实际域名"
echo "2. 请修改脚本中的 TOKEN 为有效的用户token"
echo "3. 确保数据库中存在 id=1 的盲盒数据"
echo "4. 如果没有安装Python，请安装或删除 '| python -m json.tool' 部分"
